package com.javaPro.myProject.modules.notice.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.notice.model.QTitle;
import com.javaPro.myProject.modules.notice.service.QTitleService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (公告)控制层
 *

 */
@RestController
@RequestMapping("qTitle")
public class QTitleController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private QTitleService qTitleService;
    @Resource
    private com.javaPro.myProject.common.util.uploadUtil uploadUtil;
    /**
     * 分页查询
     *
     * @param qTitle 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(QTitle qTitle) {
        startPage();
        List<QTitle> qTitles = this.qTitleService.queryByPage(qTitle);
        return getList(200,"",qTitles);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public AjaxResult queryById( Integer id) {
        return AjaxResult.success(this.qTitleService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param qTitle 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(MultipartFile file,QTitle qTitle) {
        if(file != null){
            String upload = uploadUtil.upload(file);
            qTitle.setPic(upload);
        }
        return AjaxResult.success(qTitleService.insert(qTitle));
    }

    /**
     * 编辑数据
     *
     * @param qTitle 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public AjaxResult edit(MultipartFile file, QTitle qTitle) {

            if(file != null){
                String upload = uploadUtil.upload(file);
                qTitle.setPic(upload);
            }
        return this.qTitleService.update(qTitle);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.qTitleService.deleteById(id));
    }

}

